#!/bin/bash
WOEK_NOW=$(dirname $(readlink -f "$0"))"/"
source $WOEK_NOW/config
start() {

    case $(uname -m) in
        i386)   title="ubuntu:"$(ifconfig ens33 |grep "ether" |awk '{print  $2}')"--"$(date +%Y-%m-%d-%H:%M) ;;
        x86_64) title="ubuntu:"$(ifconfig ens33 |grep "ether" |awk '{print  $2}')"--"$(date +%Y-%m-%d-%H:%M) ;;
        *)      title="imx:"$(cat /sys/fsl_otp/HW_OCOTP_CFG0)$(cat /sys/fsl_otp/HW_OCOTP_CFG1)"--"$(date +%Y-%m-%d-%H:%M) ;;
    esac

    i=0
    loop=60
    sleepTime=10 # 10*10*6=10 min 退出
    while [ $i -le $loop ]; do
        let i++
        remote_ip="www.baidu.com"
        ping -c 2  -w 5  $remote_ip > /dev/null
        if [ $? -eq 0 ] ;then
                echo  "ping "$remote_ip" ok"
                title="$title"
                txt="$(ifconfig -a | grep -i -E "ppp0|eth|mask")"
                txt="\`\`\`\n"+$txt+"\n\`\`\`";
                txt=`echo -e "${txt}"`
                curl "http://sc.ftqq.com/"$sk".send" -X POST -d "text=$title&desp=$txt"
                # ./curl_wechat.exe  "$title"  "$(ifconfig -a | grep -i -E "eth|mask")
                return  0
        else
                # echo  "ping "$remote_ip" Error, exit"
                sleep $sleepTime 
        fi
    done
    return 1
}

case "$1" in
  start)
 	echo  "Starting Push to wechat..."
	start
	;;
  stop)
	:
	;;
  restart|reload)
    : 
	;;
  *)
	echo "Usage: $0 {start|stop|restart}"
	exit 1
esac

exit $?